package com.hd.util.result;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.fasterxml.jackson.annotation.JsonIgnore;
/**
 * 分页
 * @author hzhh123
 * 2018年7月31日上午11:49:08
 */
public class Page<T> {
	 private Integer page;
	 private Integer pageSize;
	 private int total; // 总记录 
	 private List<T> rows=Collections.emptyList(); //显示的记录  
	 @JsonIgnore
	 private Map<String,Object>condition=new HashMap<String,Object>();
	public Integer getPage() {
		return page;
	}
	public void setPage(Integer page) {
		this.page = page;
	}
	public Integer getPageSize() {
		return pageSize;
	}
	public void setPageSize(Integer pageSize) {
		this.pageSize = pageSize;
	}
	public int getTotal() {
		return total;
	}
	public void setTotal(int total) {
		this.total = total;
	}
	public List<T> getRows() {
		return rows;
	}
	public void setRows(List<T> rows) {
		this.rows = rows;
	}
	
	
	public Page(Integer page, Integer pageSize) {
		super();
		this.page = page;
		this.pageSize = pageSize;
	}
	/**
	 * 计算分页
	 * @param total
	 * @return
	 */
	public int[] getPageParams(Integer total) {
		if(this.page == null || this.page < 1){
			this.page = 1;
		}
		if(this.pageSize == null || this.pageSize < 1){
			this.pageSize = 10;
		}
		int firstResult = (this.page - 1) * this.pageSize;
		int maxResult = this.pageSize;
		this.total = total;
		
		//校验分页情况
		if (firstResult >= total || firstResult < 0) {
			firstResult = 0;
			this.page = 1;
		}
		return new int[]{firstResult, maxResult};
	}
	public Map<String, Object> getCondition() {
		return condition;
	}
	public void setCondition(Map<String, Object> condition) {
		this.condition = condition;
	}
	
}
